#include "MainWindow.h"
#include "Dialogs/OptionsDialog.h"
#include "Settings.h"

#include "Editors/Graph/GraphEditor.h"
#include "Editors/Gen/GenEditor.h"
#include "Editors/CppPreview/ModelPreview.h"

#include <QtGui>

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	setupMenu();
	setupControls();

	setCentralWidget(_tab);
	setWindowTitle(tr("gen2"));

	 readSettings();
}

 void MainWindow::writeSettings()
 {
	 Settings.beginGroup("MainWindow");
	 Settings.setValue("size", size());
	 Settings.setValue("pos", pos());
	 Settings.setValue("test", 4);
	 Settings.endGroup();
 }

 void MainWindow::readSettings()
 {
	 Settings.beginGroup("MainWindow");
	 resize(Settings.value("size", QSize(400, 400)).toSize());
	 move(Settings.value("pos", QPoint(200, 200)).toPoint());

	 int test =  Settings.value("test").toInt();
	 Settings.endGroup();
 }

 void MainWindow::closeEvent(QCloseEvent *event)
 {
	 if (processChanges())
	 {
		 writeSettings();
		 event->accept();
	 } 
	 else
	 {
		 event->ignore();
	 }
 }

void MainWindow::about()
{
	QMessageBox::about(this, tr("gen2"), tr(
		"<h1>gen2</h1>"
		"<p><img src=\":/images/logo.png\"></p>"
		"<p>Object-oriented model generator.</p>"
		"<p>By <b>Vadym Hulakov</b>.</p>"
	));
}

void MainWindow::options()
{
	OptionsDialog options(this);
	options.setModal(true);
	options.exec();
}

void MainWindow::newFile()
{
	if (!processChanges())
		return;
	_tab->setTabEnabled(_modelTab, true);
	_editGraphAction->setEnabled(true);
	_tab->setCurrentIndex(_modelTab);
	_currentModel->clear();
	_genEditor->clear();
	_path = "";
}

void MainWindow::editGraph()
{
	if (!processChanges())
		return;
	_path = "";
	tabChanged(_graphTab);
	_editor = _graphEditor;
	_graphEditor->switchMode(GraphScene::Move);
	_tab->setTabEnabled(_modelTab, false);
	_editGraphAction->setEnabled(false);
}

bool MainWindow::openFile()
{
	if (!processChanges())
		return false;
				
	QString path = QFileDialog::getOpenFileName(
					this, QString(), QString(), _editor->fileFilter());

	if (path.isEmpty())
		return false;

	if (!_editor->load(path))
		return false;
	
	syncCurrentTab();

	_path = path;
	return true;
}

bool MainWindow::saveFile()
{
	if (_path.isEmpty())
		return saveAsFile();
					
	if (!_editor->save(_path))
		return false;

	return true;
}

bool MainWindow::saveAsFile()
{
	QString path = QFileDialog::getSaveFileName(
					this, QString(), QString(), _editor->fileFilter());

	if (path.isEmpty())
		return false;		

	if (!_editor->save(path))
		return false;

	_path = path;
	return true;
}

void MainWindow::tabChanged(int index)
{
	IModelEditor* newView = getEditor(index);
	if (newView == _currentView)
		return;

	if (_currentView == _editor)
		_currentModel = _editor->generateModel();

	_currentView = newView;
	if (_currentView != _editor)
		_currentView->setModel(*_currentModel);
}

void MainWindow::syncCurrentTab()
{
	_currentModel = _editor->generateModel();
	if (_currentView != _editor)
		_currentView->setModel(*_currentModel);
}

void MainWindow::setupControls()
{
	_tab = new QTabWidget();

	_genEditor = new GenEditor;
	_modelTab = _tab->addTab(_genEditor, tr("Model"));
	
	_graphEditor = new GraphEditor(true);
	_graphTab = _tab->addTab(_graphEditor, tr("Graph"));

	_cppPreview = new ModelPreview;
	_previewTab = _tab->addTab(_cppPreview, tr("Source Preview"));

	_editor = _genEditor;
	_currentView = _genEditor;
		
	connect(_tab, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
}

void MainWindow::setupMenu()
{
	QMenu *fileMenu = new QMenu(tr("&Model"), this);
	menuBar()->addMenu(fileMenu);	
	fileMenu->addAction(tr("&New"), this, SLOT(newFile()), QKeySequence::New);
	fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()), QKeySequence::Open);
	fileMenu->addAction(tr("&Save"), this, SLOT(saveFile()), QKeySequence::Save);
	fileMenu->addAction(tr("Save &As..."), this, SLOT(saveAsFile()), QKeySequence::SaveAs);
	
	fileMenu->addSeparator();
	_editGraphAction = fileMenu->addAction(tr("&Edit graph"), this, SLOT(editGraph()));

	fileMenu->addSeparator();
	fileMenu->addAction(tr("E&xit"), qApp, SLOT(quit()), QKeySequence::Quit);
	
	QMenu *toolsMenu = new QMenu(tr("&Tools"), this);
	menuBar()->addMenu(toolsMenu);
	toolsMenu->addAction(tr("&Options..."), this, SLOT(options()));

	QMenu *helpMenu = new QMenu(tr("&Help"), this);
	menuBar()->addMenu(helpMenu);
	helpMenu->addAction(tr("&About"), this, SLOT(about()));
}

IModelEditor* MainWindow::getEditor(int index)
{
	if (index == _graphTab)
		return _graphEditor;
	else if (index == _modelTab)
		return _genEditor;
	else if (index == _previewTab)
		return _cppPreview;
	return 0;
}

bool MainWindow::processChanges()
{
	if (!_editor->dirty())
		return true;

	QMessageBox box;
	box.setText(tr("The document has been modified."));
	box.setInformativeText(tr("Do you want to save your changes?"));
	box.setIcon(QMessageBox::Question); 
	box.setStandardButtons(QMessageBox::Save | 
		QMessageBox::Discard | QMessageBox::Cancel);
	switch (box.exec())
	{
	case QMessageBox::Save:
		return saveFile();
	case QMessageBox::Discard:
		return true;
	case QMessageBox::Cancel:
		return false;
	}
	return false;
}
